package operatebag.bag;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import people.Student;
import mysqloperate.*;

public class changitem extends JFrame {
    JFrame jf;
    private JLabel name;
    private JLabel sex;
    private JLabel birth;
    private JLabel math;
    private JLabel java;
    private JLabel pe;
    private JTextField nametxt;
    private JTextField sextxt;
    private JTextField birthday;
    private JTextField mathscore;
    private JTextField javascore;
    private JTextField pescore;

    private JButton changebutton;
    private JButton cancelbutton;
    public changitem(String id){
        setTitle("修改学生");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(new FlowLayout());
        setBounds(500,200,400,400);
        setResizable(false);
        setVisible(true);
        jf=this;
        Box b=Box.createVerticalBox();
        Box b1=Box.createHorizontalBox();
        name=new JLabel("姓名");
        nametxt=new JTextField(10);
        //nametxt.setText(student1.getName());
        b1.add(name);
        b1.add(nametxt);
        Box b2=Box.createHorizontalBox();
        sex=new JLabel("性别");
        sextxt=new JTextField(10);
        /*if(student1.getSEX()==1){
            sextxt.setText("男");
        }else {
            sextxt.setText("女");
        }*/
        b2.add(sex);
        b2.add(sextxt);
        Box b3=Box.createHorizontalBox();
        birth=new JLabel("出生日期");
        birthday=new JTextField(10);
        //birthday.setText(student1.getBirth());
        b3.add(birth);
        b3.add(birthday);
        Box b4=Box.createHorizontalBox();
        math=new JLabel("数学分数");
        mathscore=new JTextField(10);
        //String m=String.valueOf(student1.getMath());
        //mathscore.setText(m);
        b4.add(math);
        b4.add(mathscore);
        Box b5=Box.createHorizontalBox();
        java=new JLabel("java分数");
        javascore=new JTextField(10);
        //String j=String.valueOf(student1.getJava());
        //javascore.setText(j);
        b5.add(java);
        b5.add(javascore);
        Box b6=Box.createHorizontalBox();
        pe=new JLabel("体育分数");
        pescore=new JTextField(10);
        //String p=String.valueOf(student1.getPe());
        //pescore.setText(p);
        b6.add(pe);
        b6.add(pescore);
        Box b7=Box.createHorizontalBox();
        changebutton=new JButton("确定");
        cancelbutton=new JButton("取消");
        b7.add(changebutton);
        b7.add(cancelbutton);

        b.add(b1);
        b.add(Box.createVerticalStrut(15));
        b.add(b2);
        b.add(Box.createVerticalStrut(15));
        b.add(b3);
        b.add(Box.createVerticalStrut(15));
        b.add(b4);
        b.add(Box.createVerticalStrut(15));
        b.add(b5);
        b.add(Box.createVerticalStrut(15));
        b.add(b6);
        b.add(Box.createVerticalStrut(15));
        b.add(b7);
        this.add(b);
        changebutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String s1,s2,s3,s4,s5,s6;
                int n1,n2,n3,n4;
                s1=nametxt.getText();
                s2=sextxt.getText();
                if (s2.equals("男")){
                    n1=1;
                }
                else{
                    n1=0;
                }
                s3=birthday.getText();
                s4=mathscore.getText();
                n2=Integer.parseInt(s4);
                s5=javascore.getText();
                n3=Integer.parseInt(s5);
                s6=pescore.getText();
                n4=Integer.parseInt(s6);
                Student student=new Student();
                student.setName(s1);
                student.setSEX(n1);
                student.setBirth(s3);
                student.setMath(n2);
                student.setJava(n3);
                student.setPe(n4);

                student.setStudentid(id);
                changestumysql change=new changestumysql(student);
                try {
                    change.change();
                    JOptionPane.showMessageDialog(jf, "修改成功","",JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(jf, "修改失败","",JOptionPane.WARNING_MESSAGE);
                    throw new RuntimeException(ex);
                }
            }
        });
        cancelbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jf.dispose();
            }
        });
    }
}
